# 学生管理系统
import os.path
import sys

students = []

def save():
    with open('recode.txt', 'w', encoding='utf8') as f:
        for s in students:
            f.write(f'{s['sid']}\t{s['name']}\t{s['score']}\n')
        print('保存成功')

def load():
    if not os.path.exists('recode.txt'):
        return

    global students
    students = []
    with open('recode.txt', 'r', encoding='utf8') as f:
        for line in f:
            line = line.strip() # 默认是\n
            tokens = line.split('\t')
            student = {
                'sid':tokens[0],
                'name':tokens[1],
                'score':tokens[2]
            }
            students.append(student)
        print('加载成功')


def insert():
    print('开始新增')
    sid = input('输入学号: ')
    name = input('输入姓名: ')
    score = input('输入成绩: ')
    student = {
        'sid':sid,
        'name':name,
        'score':score
    }
    global students
    students.append(student)
    save()
    print('新增结束')

def show():
    print('显示开始')
    for student in students:
        print(f'{student['sid']}\t{student['name']}\t{student['score']}')
    print('显示结束')


def find():
    print('查找开始')
    name = input('输入要查找的学生姓名: ')
    for student in students:
        if student['name'] == name:
            print(f'{student['sid']}\t{student['name']}\t{student['score']}')
    print('查找结束')


def delete():
    print('删除开始')
    sid = input('输入要删除的学号: ')
    for student in students:
        if student['sid'] == sid:
            students.remove(student)
    save()
    print('删除结束')

def main ():
    print('-------------------------')
    print('---------学生管理系统------')
    print('-------------------------')
    load()
    while True:
        n = input('请选择: ')
        if n == '1':
            insert()
        elif n == '2':
            show()
        elif n == '3':
            find()
        elif n == '4':
            delete()
        elif n == '0':
            sys.exit(0)

        else:
            print('输入有误，请重新输入')


main()